<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <TodoHeader @addList="addList" />

    <!-- 主体部分 -->
    <TodoBody :list="renderList" @delList="delList" @changeStatus="changeStatus" />

    <!-- 底部部分 -->
    <TodoFooter :residue="residue" @clearList="clearList" :isDaneAll="isDaneAll" :type="type" @changeType="changeType"/>
  </section>
</template>

<script>
import TodoHeader from "@/components/TodoHeader.vue";
import TodoBody from "@/components/TodoBody.vue";
import TodoFooter from "@/components/TodoFooter.vue";
import { isDate } from "moment";
export default {
  components: {
    TodoHeader,
    TodoBody,
    TodoFooter,
  },
  data() {
    return {
      type:'all',
      list: [
        { id: 1, name: "吃饭", isDone: true },
        { id: 2, name: "睡觉", isDone: false },
        { id: 3, name: "打豆豆", isDone: true },
      ],
    };
  },
  methods: {
    delList(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    changeStatus(id) {
      const res = this.list.find((item) => item.id === id);
      res.isDone = !res.isDone;
    },
    addList(value) {
      const obj = { id: +new Date(), name: value, isDone: false };
      this.list = [obj, ...this.list];
    },
    clearList() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    changeType(type){
      this.type = type
    }
  },
  computed: {
    residue() {
      return this.list.filter((item) => !item.isDone).length;
    },
    isDaneAll() {
      return this.list.some((item) => item.isDone);
    },
    renderList(){
    if (this.type === 'completed') { 
      return this.list.filter(item => item.isDone)
    } else if (this.type === 'active') { 
      return this.list.filter(item => !item.isDone)
    } else {
      return this.list 
  }
    }
  },
};
</script>

<style></style>
